<template>
  <a-button :type="buttonType" ghost @click="exportExcel()">
    <BaseIcon name="CloudDownloadOutlined" :loading="printLoading" />
    <slot></slot>
  </a-button>
</template>

<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { http } from '@/utils/request'

const props = defineProps({
  searchParams: {
    type: Object,
    default: () => ({})
  },
  type: {
    type: String,
    default: '门诊'
  },
  fileType: {
    type: String,
    default: 'xlsx'
  },
  name: {
    type: String,
    default: '导出'
  },
  code: {
    type: String,
    default: ''
  },
  isShowTime: {
    type: Boolean,
    default: false
  },
  buttonType: {
    type: String,
    default: 'primary'
  }
})

const getUrl = () => {
  switch (props.type) {
    case '门诊':
      return '/outp/export'
    case '住院':
      return '/inp/export'
    case '理疗':
      return '/course/export'
    case '药库':
      return '/drugStorage/export'
    case '运维':
      return '/operation/export'
    case '检验':
      return '/lis/LisApply/exportLis'
    case '菜单管理':
      return '/common/menu/export'
    case '表头管理':
      return '/common/tableMapping/export'
    case '手术':
      return '/operation/opex/exportAppointmentRecordList'
    case '手术统计':
      return '/oprOperation/export'
    case '物资':
      return '/materials/export'
    case '报表':
      return '/statistic/Export'
    case '视光':
      return '/optometry/export'
    case '战救':
      return '/war/export'
    case '病案':
      return '/med/MedicalRecord/Export'
    case '防疫防护':
      return '/war/export'
    case '病案上报':
      return '/med/MedicalRecord/medicalRecordExport'
    case '医保':
      return '/insure/export'
    case '装备':
      return '/operation/opex/export'
  }
}
const parameters = ref({})
const printLoading = ref(false)
const exportExcel = () => {
  printLoading.value = true
  http
    .post(getUrl(), { parameters: parameters.value, tblcode: props.code, ...getExtraParams() }, { responseType: 'blob', timeout: 60 * 60 * 1000 })
    .then((res) => {
      printLoading.value = false
      message.loading('导出中...', 1)
      const href = URL.createObjectURL(res.data)
      const element = document.createElement('a')
      element.download =
        (props.name || props.type) +
        (props.isShowTime ? parameters.value.stdate.replace(/-/g, '') + '-' + parameters.value.eddate.replace(/-/g, '') : ' ') +
        '.' +
        props.fileType
      element.href = href
      element.click()
    })
    .catch(() => {
      printLoading.value = false
      message.error('导出失败，请稍后重试！')
    })
}
watch(
  () => props.searchParams,
  (val) => {
    const { dateRange, ...rest } = val
    parameters.value = {
      ...rest,
      ...(dateRange && { stdate: dateRange[0], eddate: dateRange[1] })
    }
  },
  { deep: true, immediate: true }
)
const getExtraParams = () => {
  const extraParams = {}
  const path = JSON.parse(localStorage.getItem('app')).CURRENT_ROUTE_PATH
  //易耗品参数
  if (path === '/consumable-material') {
    extraParams.mtrType = 1
  }
  if (path === '/material') {
    extraParams.mtrType = 0
  }
  return extraParams
}
</script>
<style lang="less" scoped></style>
